package site.wtcc.originalfavor.func.common.utils;

import java.io.*;

/**
 * 对象深拷贝
 * @author hxd
 */
public final class BeanCopy implements Serializable  {

    public static Object clone(Object source,Object target){
        try {
            // 将对象本身序列化到字节流
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream =
                    new ObjectOutputStream( byteArrayOutputStream );
            objectOutputStream.writeObject( source );

            // 再将字节流通过反序列化方式得到对象副本
            ObjectInputStream objectInputStream =
                    new ObjectInputStream( new ByteArrayInputStream( byteArrayOutputStream.toByteArray() ) );
            target =  objectInputStream.readObject();
            return target;

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}
